package com.hesicare.adapter.worker.pull;

import com.hesicare.adapter.entity.HisPatient;
import com.hesicare.health.entity.HisIndicatorMidinfo;
import com.hesicare.health.entity.SysPatient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

/**
 * 获取实验室检验检查
 *
 * @Author 55097
 * @create 2021/11/18 19:54
 */
@Component
public class PullindicatorOnWorker {

    private final static Logger LOGGER = LoggerFactory.getLogger(PullindicatorOnWorker.class);

    @Autowired
    @Resource(name = "jdbcTemplateSlave2")
    private JdbcTemplate jdbcTemplate;

    final static SimpleDateFormat DATE = new SimpleDateFormat("yyyy-MM-dd");
    final static SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public List<HisIndicatorMidinfo> readIndcatorList(SysPatient sysPatient) {
        List<HisIndicatorMidinfo> hisIndicatorMidinfoList = new ArrayList<>();
        List<Map> projectList = new ArrayList<>();
        StringBuffer sql = new StringBuffer()
                .append("EXEC dbo.USP_XT_LIS_PATIENT @StartDate ='" + new StringBuilder(DATE.format(new Date())).append(" 00:00:00").toString() + "' ,@EndDate='" + new StringBuilder(DATE.format(new Date())).append(" 23:59:59").toString() + "',@PATID='" + sysPatient.getHospitalizedNum() + "'");
        try {
            projectList = this.jdbcTemplate.query(sql.toString(), new RowMapper<Map>() {
                @Override
                public Map mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map rowMap = new HashMap(8);
                    rowMap.put("specimen_no", rs.getString("标本号"));
                    rowMap.put("project_name", rs.getString("项目"));
                    rowMap.put("inspection_time", rs.getString("送检时间"));
                    rowMap.put("detection_time", rs.getString("日期"));
                    rowMap.put("doctor_name", rs.getString("检验医生"));
                    rowMap.put("audit_doctor_name", rs.getString("审核医生"));
                    rowMap.put("audit_tag", rs.getString("审核标记"));
                    rowMap.put("report_tag", rs.getString("报告标记"));
                    return rowMap;
                }
            });
            if (projectList.size() < 1) {
                return null;
            }
            for (Map map : projectList) {
                getIndocator(sysPatient, hisIndicatorMidinfoList, map);
            }
        } catch (Exception e) {
            LOGGER.error("拉取实验室检查项错误", errorTrackSpace(e));
        }
        return hisIndicatorMidinfoList;
    }

    private void getIndocator(SysPatient sysPatient, List<HisIndicatorMidinfo> hisIndicatorMidinfoList, Map map) {
        List<Map> indicatorList = new ArrayList<>();
        StringBuffer indocatorSql = new StringBuffer()
                .append("EXEC dbo.USP_XT_LIS_RESULT @inspection_id = '" + map.get("specimen_no").toString() + "',@TYPE='0'");
        try {
            indicatorList = this.jdbcTemplate.query(indocatorSql.toString(), new RowMapper<Map>() {
                @Override
                public Map mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map rowMap = new HashMap(8);
                    rowMap.put("specimen_no", rs.getString("标本号"));
                    rowMap.put("type_no", rs.getString("序号"));
                    rowMap.put("type", rs.getString("项目代码"));
                    rowMap.put("type_name", rs.getString("项目名称"));
                    rowMap.put("inspection_result", rs.getString("检验结果"));
                    rowMap.put("reference", rs.getString("参考值"));
                    rowMap.put("unit", rs.getString("单位"));
                    rowMap.put("result", rs.getString("标志"));
                    return rowMap;
                }
            });
            for (Map indicatorMap : indicatorList) {
                HisIndicatorMidinfo hisIndicatorMidinfo = new HisIndicatorMidinfo();
                hisIndicatorMidinfo.setSpecimenNo(map.get("specimen_no").toString());
                hisIndicatorMidinfo.setProjectName(map.get("project_name").toString());
                hisIndicatorMidinfo.setInspectionTime(DATE_TIME.parse(map.get("inspection_time").toString()));
                hisIndicatorMidinfo.setDetectionTime(DATE_TIME.parse(map.get("detection_time").toString()));
                hisIndicatorMidinfo.setDoctorName(map.get("doctor_name").toString());
                hisIndicatorMidinfo.setAuditDoctorName(map.get("audit_doctor_name").toString());
                hisIndicatorMidinfo.setAuditTag(map.get("audit_tag").toString());
                hisIndicatorMidinfo.setReportTag(map.get("report_tag").toString());
                if (indicatorMap.get("type_no") != null) {
                    hisIndicatorMidinfo.setTypeNo(indicatorMap.get("type_no").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("type") != null) {
                    hisIndicatorMidinfo.setType(indicatorMap.get("type").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("type_name") != null) {
                    hisIndicatorMidinfo.setTypeName(indicatorMap.get("type_name").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("inspection_result") != null) {
                    hisIndicatorMidinfo.setInspectionResult(indicatorMap.get("inspection_result").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("reference") != null) {
                    hisIndicatorMidinfo.setReference(indicatorMap.get("reference").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("unit") != null) {
                    hisIndicatorMidinfo.setUnit(indicatorMap.get("unit").toString());
                } else {
                    continue;
                }
                if (indicatorMap.get("result") != null) {
                    hisIndicatorMidinfo.setResult(indicatorMap.get("result").toString());
                }
                hisIndicatorMidinfo.setHospitalizedNum(sysPatient.getHospitalizedNum());
                hisIndicatorMidinfo.setHisPatientId(sysPatient.getHisPatientId());
                hisIndicatorMidinfo.setPatientId(sysPatient.getPatientId());
                hisIndicatorMidinfo.setCreatDate(new Date());
                hisIndicatorMidinfoList.add(hisIndicatorMidinfo);
            }
        } catch (Exception e) {
            LOGGER.error("拉取检查项错误", errorTrackSpace(e));
        }
    }
}
